# -*- coding: utf-8 -*-
import pendulum
from datetime import timedelta
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from airflow.exceptions import AirflowSkipException
from jms_point_hi.dwd.dwd_wide_yl_rt_ssmx_bill_detail_hf import jms_dwd__dwd_wide_yl_rt_ssmx_bill_detail_hf

cst = pendulum.timezone('Asia/Shanghai')

class TrackRichSqlSensor(SparkSqlOperator):

    def pre_execute(self, context):
        day = cst.convert(context['ti'].execution_date) + timedelta(hours=1)

        schedule_date = ['05']

        if day.strftime('%H') not in schedule_date:
            print(f'{day.strftime("%H")} not in {schedule_date}, should skip')
            super().pre_execute(context)
            raise AirflowSkipException()
        else:
            print(f'{day.strftime("%H")} in {schedule_date}, run now')
            super().pre_execute(context)


jms_dwd__dwd_track_broken_detail = TrackRichSqlSensor(
    task_id='jms_dwd__dwd_track_broken_detail',
    email=['jarl.huang@jtexpress.com','yl_etl@yl-scm.com'],
    master='yarn',
    name='jms_dwd__dwd_track_broken_detail_{{ execution_date | cst_hour }}',
    sql='jms_point_hi/dwd/dwd_track_broken_detail/execute.sql',
    pool_slots=2,
    driver_cores=4,
    driver_memory='4g',
    #executor_cores=4,
    executor_cores=2,
    #executor_memory='12G',
    executor_memory='8G',
    #num_executors=30,
    num_executors=16,
    conf={'spark.dynamicAllocation.enabled'                  : 'true',
          'spark.shuffle.service.enabled'                    : 'true',
          #'spark.dynamicAllocation.maxExecutors'             : 40,
          'spark.dynamicAllocation.maxExecutors'             : 20,
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 600,
          'spark.network.timeout' : 600,
          'spark.sql.sources.partitionOverwriteMode'         : 'dynamic',
          'spark.executor.memoryOverhead'                    : '2G',
          'spark.sql.shuffle.partitions'                     : 400
          },
    hiveconf={'hive.exec.dynamic.partition'             : 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode'        : 'nonstrict',
              'hive.exec.max.dynamic.partitions'        : 200,  # 最大分区
              'hive.exec.max.dynamic.partitions.pernode': 200,  # 最大分区
              },
    yarn_queue='pro_hi',
    execution_timeout=timedelta(minutes=120),
    retries=0,
    sla=timedelta(hours=2))


jms_dwd__dwd_track_broken_detail << jms_dwd__dwd_wide_yl_rt_ssmx_bill_detail_hf



